#include "Animation.h"
#include "Timer.h"
//---------------------------------------------------------------------------------------------//
Animation::Animation(D3DVertexUV* vertices,Texture* pTexture, float frameWidth, float frameHeight, unsigned int cant)
:
	 m_pVertices(vertices),
	 m_pTexture(pTexture),
	 m_frameWidth(frameWidth),
	 m_frameHeight(frameHeight),
	 m_cant(cant),
	 m_frameNumber(0),
	 m_miliSecondsAcum(0)
{}
//---------------------------------------------------------------------------------------------//
void Animation::GetCurrentUV(unsigned int frameNumber)
{	 
	//cantidad frames x y
	int framesX = m_pTexture->width  / m_frameWidth; 
	int framesY = m_pTexture->height / m_frameHeight;

	int x = frameNumber % framesX; 
	int y = frameNumber / framesX;

	float xPixels = x * m_frameWidth;
	float yPixels = y * m_frameHeight;
	
	float U = xPixels / m_pTexture->width;
	float V = yPixels / m_pTexture->height;
	
	float displacedU = (xPixels + m_frameWidth) / m_pTexture->width;
	float displacedV = (yPixels + m_frameWidth) / m_pTexture->height;

	m_pVertices[0].SetUV(U,V);
	m_pVertices[1].SetUV(displacedU,V);
	m_pVertices[2].SetUV(U,displacedV);
	m_pVertices[3].SetUV(displacedU,displacedV);
}
//---------------------------------------------------------------------------------------------//
void Animation::AnimateSprite(float frameRate)
{ 
	m_miliSecondsAcum += Timer::GetDT();
	
	if(m_miliSecondsAcum >= frameRate)
	{
		m_miliSecondsAcum = 0;
		
		if(m_frameNumber >= m_cant)
			m_frameNumber = 0;
		else
			m_frameNumber++;
	}

	GetCurrentUV(m_frameNumber);
}